<template>
  <div class="dataview">
    <!-- <h-tooltip class="cancel-btn" title="这个页面接口应该也没好" /> -->
    <!-- <br>
    <br> -->
    <h-ccard style="box-shadow: 0px 0px 8px 0px rgba(0,76,146,0.1000);">
      <h-query
        format="YYYY-MM-DD"
        :showTime="false"
        style="margin-bottom: 0;"
        :defaultValueRang="defaultValueRang"
        :hideBtn="true"
        :showKey="['startTime,endTime']"
        @search="search">
        <a-radio-group slot="btn" class="time-query" buttonStyle="solid" v-model="time" @change="change">
          <a-radio-button v-for="item in queryTime" :key="item.value" :value="item.value">{{ item.label }}</a-radio-button>
        </a-radio-group>
      </h-query>
    </h-ccard>
    <div class="dataview-card">
      <a-row :gutter="60">
        <a-col :span="6" v-for="item in card" :key="item.id">
          <h-ccard>
            <div class="dataview-card-nav">
              <h1>{{ item.title }}</h1>
              <p><span>{{ totalData[item.key] }}</span>{{ item.unit }}</p>
              <p>{{ rangeTime }}</p>

              <div class="dataview-icon">
                <img :style="item.style ? item.style : {}" :src="require(`@/assets/Image/icon/${item.icon}.png`)" alt="">
              </div>
            </div>
          </h-ccard>
        </a-col>
      </a-row>
    </div>
    <div class="dataview-card">
      <a-row :gutter="60">
        <a-col :span="12">
          <h-ccard :hideBd="true" title="不同等级事件占比">
            <template slot="right">
              {{ rangeTime }}
            </template>
            <div style="height: 300px">
              <Pie v-if="show" :data="pieData" />
            </div>
          </h-ccard>
        </a-col>
        <a-col :span="12">
          <h-ccard :hideBd="true" title="接受数据量（条）">
            <template slot="right">
              {{ rangeTime }}
            </template>
            <div style="height: 300px">
              <Bar v-if="showBar" :data="barData" />
            </div>
          </h-ccard>
        </a-col>
      </a-row>
    </div>
    <div class="dataview-card">
      <a-row :gutter="60">
        <a-col :span="24">
          <h-ccard :hideBd="true" title="第三方公司质量评价">
            <div style="margin-left: -60px">
              <a-row :gutter="[120, 40]">
                <a-col :span="12" v-for="(item, i) in recommentScore" :key="i">
                  <h-ccard class="dataview-com" shadow>
                    <div class="dataview-tip">
                      {{ quality[item.quality] }}
                    </div>
                    <h1>{{ item.companyName }}</h1>
                    <div class="dataview-com-text">
                      <a-row :gutter="20" >
                        <a-col :span="8">
                          <p>接受指令次数</p>
                          <p>{{ item.receive }}</p>
                        </a-col>
                        <a-col :span="8">
                          <p>数据次数</p>

                          <p>{{ item.escalation }}</p>
                        </a-col>
                        <a-col :span="8">
                          <a-progress strokeColor="#1890ff" class="dataview-circle" type="circle" :percent="(item.escalation / item.receive) * 100">
                            <template #format="percent">
                              <p style="color: #1890ff">采集效率</p>

                              <p>{{ (item.escalation / item.receive) * 100 }}%</p>
                            </template>
                          </a-progress>
                          <!-- <h-tooltip title="采集效率 如何计算" /> -->
                        </a-col>
                      </a-row>
                    </div>
                  </h-ccard>
                </a-col>
              </a-row>
              <div class="hlist-pagination" style="">
                <a-pagination v-model="current" v-bind="paginationConfig" @change="changePage" />
              </div>
            </div>
          </h-ccard>
        </a-col>
      </a-row>
    </div>
  </div>
</template>

<script>
import Pie from './charts/Pie/Index';
import Bar from './charts/Bar/Index';
import { getDataStatistics, getDataStatisticsTime, getAcceptData, getAcceptDataTime, componeyList } from '@/api/serve';
import moment from 'moment';
export default {
  components: {
    Pie,
    Bar
  },
  data() {
    return {
      current: 1,
      paginationConfig: {
        total: 0,
        pageSize: 4
      },
      show: false,
      showBar: false,
      start: '',
      end: '',
      time: 0,
      totalData: {},
      type: 'change',
      timeRang: {
        0: {
          start: moment().subtract(6, 'days').format('YYYY/MM/DD'),
          end: moment().format('YYYY/MM/DD')
        },
        1: {
          start: moment().subtract(29, 'days').format('YYYY/MM/DD'),
          end: moment().format('YYYY/MM/DD')
        },
        2: {
          start: moment().subtract(3, 'month').format('YYYY/MM/DD'),
          end: moment().format('YYYY/MM/DD')
        },
        3: {
          start: moment().subtract(6, 'month').format('YYYY/MM/DD'),
          end: moment().format('YYYY/MM/DD')
        },
        4: {
          start: moment().subtract(12, 'month').format('YYYY/MM/DD'),
          end: moment().format('YYYY/MM/DD')
        }
      },
      pieData: [],
      barData: [],
      quality: {
        1: '普通',
        2: '良好',
        3: '优秀'
      },
      defaultValueRang: [],
      queryTime: [
        {
          label: '一周',
          value: 0
        },
        {
          label: '一月',
          value: 1
        },
        {
          label: '一季度',
          value: 2
        },
        {
          label: '半年',
          value: 3
        },
        {
          label: '一年',
          value: 4
        }
      ],
      card: [
        {
          id: 1,
          title: '值班记录数量',
          num: 0,
          unit: '条',
          time: '2022-01-20~2022-01-26',
          icon: 'd1',
          key: 'dutyRecord'
        },
        {
          id: 2,
          title: '日常事件总数量',
          num: 0,
          unit: '条',
          time: '2022-01-20~2022-01-26',
          icon: 'd2',
          key: 'routineMatters'
        },
        {
          id: 3,
          title: '周期指令下发数量',
          num: 0,
          unit: '条',
          time: '2022-01-20~2022-01-26',
          icon: 'd3',
          key: 'cycleInstruction',
          style: {
            width: '26px',
            marginLeft: '4px'
          }
        },
        {
          id: 4,
          title: '事件持续平均时长',
          num: 0,
          unit: '小时',
          time: '2022-01-20~2022-01-26',
          icon: 'd4',
          key: 'averageDuration'
        }
      ],
      recommentScore: [
        // {
        //   title: '工业互联网游戏公司',
        //   n1: 125,
        //   n2: 300,
        //   percent: 75,
        //   status: '良好'
        // }
      ]

    };
  },
  computed: {
    rangeTime() {
      return this.type == 'change' ? `${this.timeRang[this.time].start}~${this.timeRang[this.time].end}` : `${this.start}~${this.end}`;
    }
  },
  created() {
    this.getDataStatistics();
    this.getAcceptData();

    this.start = this.timeRang[this.time].start.replace(/\//g, '-');
    this.end = this.timeRang[this.time].end.replace(/\//g, '-');
    this.defaultValueRang = [`${this.start}`, `${this.end}`];
    this.componeyList();
  },
  methods: {
    search({ startTime, endTime }) {
      this.type = 'search';
      this.start = moment(startTime).format('YYYY-MM-DD');
      this.end = moment(endTime).format('YYYY-MM-DD');
      this.getDataStatisticsTime();
      this.getAcceptDataTime();
      this.time = '';
      this.current = 1;
      this.componeyList(1);
    },
    change(e) {
      this.time = e.target.value;
      this.getDataStatistics();
      this.getAcceptData();
      this.type = 'change';
      this.current = 1;
      this.start = this.timeRang[this.time].start.replace(/\//g, '-');
      this.end = this.timeRang[this.time].end.replace(/\//g, '-');
      this.defaultValueRang = [`${this.start}`, `${this.end}`];

      this.componeyList(1);

      // console.log(this.rangeTime);
    },
    getDataStatistics() {
      getDataStatistics({
        sorce: this.time
      }).then(res => {
        this.totalData = res.data;
        this.setpieData();
      });
    },
    getDataStatisticsTime() {
      getDataStatisticsTime({
        start: this.start,
        end: this.end
      }).then(res => {
        this.totalData = res.data;
        this.setpieData();
      });
    },
    getAcceptData() {
      getAcceptData({
        sorce: this.time
      }).then(res => {
        this.barData = res.data.map(e => {
          return {
            name: e.date,
            value: Number(e.sum)
          };
        });
        this.refreshBar();
      });
    },
    getAcceptDataTime() {
      getAcceptDataTime({
        start: this.start,
        end: this.end
      }).then(res => {
        this.barData = res.data.map(e => {
          return {
            name: e.date,
            value: Number(e.sum)
          };
        });
        this.refreshBar();
      });
    },
    refresh() {
      this.show = false;
      this.$nextTick(() => {
        this.show = true;
      });
    },
    refreshBar() {
      this.showBar = false;
      this.$nextTick(() => {
        this.showBar = true;
      });
    },
    setpieData() {
      const key = {
        ordinary: '普通预警',
        intermediate: '中级预警',
        senior: '高级预警',
        super: '特级预警'
      };

      this.pieData = Object.keys(key).map(e => {
        return {
          name: key[e],
          value: this.totalData[e]
        };
      });

      this.refresh();
    },
    componeyList(pageNo) {
      componeyList({
        pageNo: pageNo || 1,
        pageSize: this.paginationConfig.pageSize,
        start: this.start,
        end: this.end
      }).then(res => {
        this.recommentScore = res.data.records;
        this.paginationConfig.total = res.data.total;
      });
    },
    changePage(pageNo) {
      this.componeyList(pageNo);
    }
  }
};
</script>

<style scoped lang="less">
  .time-query {
    margin-top: 6px;
    .ant-radio-button-wrapper {
      border-radius: 30px;
      margin-left: 12px;
      padding: 0 30px;
      border: 1px solid #4071FB;
      color: #4071FB;
      // height: 40px;
      // line-height: 40px;
      font-size: 16px;
    }
    .ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled) {
      color: #fff !important;
      background: #4071FB !important;
    }
    .ant-radio-button-wrapper::before {
      display: none;
    }
  }
  .dataview-card {
    margin-left: -30px;
    margin-top: 26px;
  }
  .dataview-card-nav {
    padding: 0 12px;
    position: relative;
    >h1 {
      font-size: 18px;
      font-weight: 400;
      color: #999999;
    }
    >p:nth-child(2) {
      font-size: 18px;
      font-weight: bold;
      color: #333333;
      >span {
        font-size: 28px;
      }
    }
    >p:last-child {
      font-size: 14px;
      font-weight: 400;
      color: #999999;
    }
    .dataview-icon {
      width: 53px;
      height: 53px;
      background: #4071FB;
      border-radius: 5px;
      position: absolute;
      right: 0;
      top: 50%;
      transform: translate(0, -50%);
      text-align: center;
      line-height: 48px;
      >img {
        width: 35px;
      }
    }
  }

  .dataview-com {
    .dataview-tip {
      width: 76px;
      height: 32px;
      background: #1F3B8A;
      // border-radius: 10px;
      position: absolute;
      right: 0;
      top: 0;
      font-size: 16px;
      font-weight: 400;
      color: #FFFFFF;
      text-align: center;
      line-height: 32px;
    }
    >h1 {
      font-size: 20px;
      font-weight: 400;
      color: #333;
    }
    .dataview-com-text {
      // text-align: center;
      font-size: 16px;
      color: #999;
      margin-top: 20px;
      margin-left: -10px;
      p:nth-child(2) {
        font-size: 30px;
        color: #333;
        font-weight: 600;
      }
    }
    .dataview-circle {
      margin-top: -40px;
      .ant-progress-text {
        >p:first-child {
          font-size: 16px;
          margin-bottom: 12px;
        }
        >p:last-child {
          color: #1890ff;
        }
      }
    }
  }

  .hlist-pagination {
    margin: 40px 0;
    text-align: center;
    margin-top: 60px;
  }

  h1,p {
    margin: 0;
  }

</style>
